home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / PropertyEditorSupport.java < prev    next >
Text File  |  1998-09-22  |  8KB  |  239 lines

  1. /*
  2.  * @(#)PropertyEditorSupport.java    1.7 98/07/01
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.beans;
  16.  
  17. /**
  18.  * This is a support class to help build property editors.
  19.  * <p>
  20.  * It can be used either as a base class or as a delagatee.
  21.  */
  22.  
  23. import java.beans.*;
  24.  
  25. public class PropertyEditorSupport implements PropertyEditor {
  26.  
  27.     /**
  28.      * Constructor for use by derived PropertyEditor classes.
  29.      */
  30.  
  31.     protected PropertyEditorSupport() {
  32.     source = this;
  33.     }
  34.  
  35.     /**
  36.      * Constructor for use when a PropertyEditor is delegating to us.
  37.      * @param source  The source to use for any events we fire.
  38.      */
  39.  
  40.     protected PropertyEditorSupport(Object source) {
  41.     this.source = source;
  42.     }
  43.  
  44.     /**
  45.      * Set (or change) the object that is to be edited.
  46.      * @param value The new target object to be edited.  Note that this
  47.      *     object should not be modified by the PropertyEditor, rather 
  48.      *     the PropertyEditor should create a new object to hold any
  49.      *     modified value.
  50.      */
  51.     public void setValue(Object value) {
  52.     this.value = value;
  53.     firePropertyChange();
  54.     }
  55.  
  56.     /**
  57.      * @return The value of the property.
  58.      */
  59.  
  60.     public Object getValue() {
  61.     return value;
  62.     }
  63.  
  64.     //----------------------------------------------------------------------
  65.  
  66.     /**
  67.      * @return  True if the class will honor the paintValue method.
  68.      */
  69.  
  70.     public boolean isPaintable() {
  71.     return false;
  72.     }
  73.  
  74.     /**
  75.      * Paint a representation of the value into a given area of screen
  76.      * real estate.  Note that the propertyEditor is responsible for doing
  77.      * its own clipping so that it fits into the given rectangle.
  78.      * <p>
  79.      * If the PropertyEditor doesn't honor paint requests (see isPaintable)
  80.      * this method should be a silent noop.
  81.      *
  82.      * @param gfx  Graphics object to paint into.
  83.      * @param box  Rectangle within graphics object into which we should paint.
  84.      */
  85.     public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) {
  86.     }
  87.  
  88.     //----------------------------------------------------------------------
  89.  
  90.     /**
  91.      * This method is intended for use when generating Java code to set
  92.      * the value of the property.  It should return a fragment of Java code
  93.      * that can be used to initialize a variable with the current property
  94.      * value.
  95.      * <p>
  96.      * Example results are "2", "new Color(127,127,34)", "Color.orange", etc.
  97.      *
  98.      * @return A fragment of Java code representing an initializer for the
  99.      *       current value.
  100.      */
  101.     public String getJavaInitializationString() {
  102.     return "???";
  103.     }
  104.  
  105.     //----------------------------------------------------------------------
  106.  
  107.     /**
  108.      * @return The property value as a string suitable for presentation
  109.      *       to a human to edit.
  110.      * <p>   Returns "null" is the value can't be expressed as a string.
  111.      * <p>   If a non-null value is returned, then the PropertyEditor should
  112.      *         be prepared to parse that string back in setAsText().
  113.      */
  114.     public String getAsText() {
  115.     if (value instanceof String) {
  116.         return (String)value;
  117.     }
  118.     return ("" + value);
  119.     }
  120.  
  121.     /**
  122.      * Set the property value by parsing a given String.  May raise
  123.      * java.lang.IllegalArgumentException if either the String is
  124.      * badly formatted or if this kind of property can't be expressed
  125.      * as text.
  126.      * @param text  The string to be parsed.
  127.      */
  128.     public void setAsText(String text) throws java.lang.IllegalArgumentException {
  129.     if (value instanceof String) {
  130.         setValue(text);
  131.         return;
  132.     }
  133.     throw new java.lang.IllegalArgumentException(text);
  134.     }
  135.  
  136.     //----------------------------------------------------------------------
  137.  
  138.     /**
  139.      * If the property value must be one of a set of known tagged values, 
  140.      * then this method should return an array of the tag values.  This can
  141.      * be used to represent (for example) enum values.  If a PropertyEditor
  142.      * supports tags, then it should support the use of setAsText with
  143.      * a tag value as a way of setting the value.
  144.      *
  145.      * @return The tag values for this property.  May be null if this 
  146.      *   property cannot be represented as a tagged value.
  147.      *    
  148.      */
  149.     public String[] getTags() {
  150.     return null;
  151.     }
  152.  
  153.     //----------------------------------------------------------------------
  154.  
  155.     /**
  156.      * A PropertyEditor may chose to make available a full custom Component
  157.      * that edits its property value.  It is the responsibility of the
  158.      * PropertyEditor to hook itself up to its editor Component itself and
  159.      * to report property value changes by firing a PropertyChange event.
  160.      * <P>
  161.      * The higher-level code that calls getCustomEditor may either embed
  162.      * the Component in some larger property sheet, or it may put it in
  163.      * its own individual dialog, or ...
  164.      *
  165.      * @return A java.awt.Component that will allow a human to directly
  166.      *      edit the current property value.  May be null if this is
  167.      *        not supported.
  168.      */
  169.  
  170.     public java.awt.Component getCustomEditor() {
  171.     return null;
  172.     }
  173.  
  174.     /**
  175.      * @return  True if the propertyEditor can provide a custom editor.
  176.      */
  177.     public boolean supportsCustomEditor() {
  178.     return false;
  179.     }
  180.   
  181.     //----------------------------------------------------------------------
  182.  
  183.     /**
  184.      * Register a listener for the PropertyChange event.  The class will
  185.      * fire a PropertyChange value whenever the value is updated.
  186.      *
  187.      * @param listener  An object to be invoked when a PropertyChange
  188.      *        event is fired.
  189.      */
  190.     public synchronized void addPropertyChangeListener(
  191.                 PropertyChangeListener listener) {
  192.     if (listeners == null) {
  193.         listeners = new java.util.Vector();
  194.     }
  195.     listeners.addElement(listener);
  196.     }
  197.  
  198.     /**
  199.      * Remove a listener for the PropertyChange event.
  200.      *
  201.      * @param listener  The PropertyChange listener to be removed.
  202.      */
  203.     public synchronized void removePropertyChangeListener(
  204.                 PropertyChangeListener listener) {
  205.     if (listeners == null) {
  206.         return;
  207.     }
  208.     listeners.removeElement(listener);
  209.     }
  210.  
  211.     /**
  212.      * Report that we have been modified to any interested listeners.
  213.      *
  214.      * @param source  The PropertyEditor that caused the event.
  215.      */
  216.     public void firePropertyChange() {
  217.     java.util.Vector targets;
  218.     synchronized (this) {
  219.         if (listeners == null) {
  220.             return;
  221.         }
  222.         targets = (java.util.Vector) listeners.clone();
  223.     }
  224.     // Tell our listeners that "everything" has changed.
  225.         PropertyChangeEvent evt = new PropertyChangeEvent(source, null, null, null);
  226.  
  227.     for (int i = 0; i < targets.size(); i++) {
  228.         PropertyChangeListener target = (PropertyChangeListener)targets.elementAt(i);
  229.         target.propertyChange(evt);
  230.     }
  231.     }
  232.  
  233.     //----------------------------------------------------------------------
  234.  
  235.     private Object value;
  236.     private Object source;
  237.     private java.util.Vector listeners;
  238. }
  239.